﻿
namespace WeixinSDK.API
{
    /// <summary>
    /// API帮助类
    /// </summary>
    static class WxApiHelper
    {
        public const string urlFormat = "https://{0}api.weixin.qq.com/cgi-bin/{1}/{2}?access_token={3}{4}";

        public static string HttpPost(string access_token, string categroy, string verb, string jsonData, string queryExt = null)
        {
            var url = string.Format(urlFormat, string.Empty, categroy, verb, access_token, queryExt);

            Program.logger.Debug(" [API:Post From Url] " + url);

            return WxWebClient.HttpPost(url, jsonData);
        }

        public static string HttpPost(string access_token, Categroy categroy, Verb verb, string jsonData, string queryExt = null)
        {
            var url = string.Format(urlFormat, string.Empty, categroy, verb, access_token, queryExt);

            Program.logger.Debug(" [API:Post From Url] " + url);

            return WxWebClient.HttpPost(url, jsonData);
        }

        public static string HttpGet(string access_token, Categroy categroy, Verb verb, string queryExt = null)
        {
            var url = string.Format(urlFormat, string.Empty, categroy, verb, access_token, queryExt);

            Program.logger.Debug(" [API:Get From Url] " + url);

            return WxWebClient.HttpGet(url);
        }

        public static string HttpGet(string access_token, string categroy, string verb, string queryExt = null)
        {
            var url = string.Format(urlFormat, string.Empty, categroy, verb, access_token, queryExt);

            Program.logger.Debug(" [API:Get From Url] " + url);

            return WxWebClient.HttpGet(url);
        }

        /// <summary>
        /// 构建访问凭据api请求的url
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="secret"></param>
        /// <returns></returns>
        public static string BuildAccessTokenUrl(string appid, string secret)
        {
            return string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, secret);
        }

        /// <summary>
        /// 构建微信API请求Url
        /// </summary>
        /// <param name="categroy">类别</param>
        /// <param name="verb">动作</param>
        /// <param name="access_token">访问凭证</param>
        /// <param name="queryExt">其它请求字符串参数对</param>
        /// <returns>根据参数创建好的Url字符串</returns>
        public static string BuildApiUrl(Categroy categroy, Verb verb, string access_token, string queryExt = null)
        {
            return string.Format(urlFormat,
                (categroy == Categroy.media ? "file." : string.Empty),
                categroy, verb, access_token, queryExt);
        }

        /// <summary>
        /// api处理类别
        /// </summary>
        public enum Categroy { menu, media, groups, user,qrcode }

        /// <summary>
        /// api动作
        /// </summary>
        public enum Verb { create, get, delete, update, getid, upload, info }
    }
}
